﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using XcoAppSpaces.Contracts.Messaging;

namespace XcoAppSpaces.Serialization.Binary
{
	class WorkerMessageSerializationSurrogate : ISerializationSurrogate
	{
		public void GetObjectData(object obj, SerializationInfo info, StreamingContext context)
		{
			var msg = (WorkerMessage)obj;
			info.AddValue("WorkerIdentifier", msg.WorkerIdentifier);
			info.AddValue("SessionID", msg.SessionID);
			info.AddValue("Msg", msg.Msg);
		}

		public object SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
		{
			obj = new WorkerMessage(
				(string)info.GetValue("WorkerIdentifier", typeof(string)),
				(Guid)info.GetValue("SessionID", typeof(Guid)),
				info.GetValue("Msg", typeof(object))
				);
			return obj;
		}
	}
}